#ifndef RAYTRACER_H_
#define RAYTRACER_H_

#include "../math/Vector.h"
#include "../math/Ray.h"

#include "../camera/Camera.h"
#include "../stdafx.h"
#include "../timer/Timer.h"

#include <opencv/cv.h>
#include <opencv/highgui.h>

#define RAY_TRACE_MAX_DEPTH 10
class RayTracer
{
public:
	RayTracer();
	virtual ~RayTracer();
	
	virtual void Update(int time);
	virtual void Render();
	
protected:
	Camera* camera;
	IplImage* rayImage;
	byte* rayPixels;
	float* floatPixels;
	
	int width;
	int height;
	int bpp;
	int frameId;
	Timer timer;
		
protected: /* eye */
	Vector3f eye;	
	float zNear;
protected: /* field of view */
	void ComputeFieldOfView();
	
	float fovX, fovY;
	float viewPlaneWidth;
	float viewPlaneHeight;
	Vector3f topleftAnchor;
	float dx, dy; /* delta to shift in the world plane during two pixels on the image */
	
protected: /* ray tracing */
	Ray3f ray;	 
protected: /* render a few popular objects */
	void RenderSphere();

	void Trace(Ray3f ray, Vector3f& color, int depth);
	
protected: /* objects management */
	Object** objects;
	int objectCount;
protected: /* video writer */
	void OpenWriter(const char* file, float fps, int width, int height);
	void CloseWriter();
	void WriteFrame();	
	void WriteImage();
	
	CvVideoWriter* writer;
	
	
public: /* main */
	void Main();
private:
	void OnMainLoop();
};

#endif /*RAYTRACER_H_*/
